package com.cskaoyan.lz_service;

import com.cskaoyan.bean.Feedback;
import com.cskaoyan.bean.SubmitMessageBO;
import com.cskaoyan.bean.lzpackage.feedback.FeedBackData;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.mapper.FeedbackMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FeedBackServiceImpl implements FeedBackService{

    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public FeedBackData query(BaseParam param, String username,String id) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        List<Feedback> list = feedbackMapper.select(sort,order,username,id);
        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<Feedback> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return FeedBackData.data(total,pages,limit,page,list);
    }
    /**
     * @Description:
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 11:04
     * @Param: [submitMessageBO, userId, username]:
     * @return: int:
     **/
    @Override
    public int submit(SubmitMessageBO submitMessageBO, Integer userId, String username) {
        Feedback feedback = new Feedback(null, userId, username, submitMessageBO.getMobile(),
                submitMessageBO.getFeedType(), submitMessageBO.getContent(), null, submitMessageBO.getHasPicture(),
                submitMessageBO.getPicUrls(), new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()),
                false);
        int affectedRows = feedbackMapper.insertSelective(feedback);
        if (affectedRows==1){
            return 200;
        }
        return 0;
    }
}
